package com.vanzo.some.interphone;

import android.app.Fragment;
import android.content.Intent;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.vanzo.some.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tisen on 2017/12/26.
 */

public class ConnectFragment extends Fragment {
    private static final String TAG = "ConnectFragment";
    protected View view;
    //    private TextView view_title;
    private TextView count;
    private ImageView addConnect;
    private RecyclerView recyclerView;
    private ConnectAdapter adapter;
    private List<Connect> connects = new ArrayList<>();
    private LinearLayoutManager layoutManager;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_connect, container, false);
        count = (TextView) view.findViewById(R.id.connect_count);
        addConnect = (ImageView) view.findViewById(R.id.connect_add);
        recyclerView = (RecyclerView) view.findViewById(R.id.connect_recycler);
        adapter = new ConnectAdapter(getContext());
        setRecyclerViewListener();
        setAddConnectListener();
        layoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
        return view;
    }

    private void setRecyclerViewListener() {
        adapter.setItemOnClickListener(new ConnectAdapter.ItemOnClickListener() {
            @Override
            public void onClick(View view, int position) {
                Toast.makeText(getContext(), position + "", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void setAddConnectListener() {
        addConnect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), EditConnectActivity.class);
                startActivity(intent);
//                if (adapter.getItemCount() >= 20) {
//                    Toast.makeText(getContext(), "频道列表已满", Toast.LENGTH_SHORT).show();
//                } else {
//
//                }
            }
        });
    }

    private void addConnect(Connect connect) {
        adapter.addConnect(connect);
        count.setText(adapter.getItemCount() + "/20");
    }

    private void updateConnect(int position, Connect connect) {
        adapter.updateConnect(position, connect);
    }

}
